﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// BigInteger类型转换接口
    /// </summary>
    public class BigIntegerTypeConverter : TypeConverterAbs
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public BigIntegerTypeConverter()
            : base(PrimitiveDataTypes.BigIntegerType)
        {

        }

        /// <summary>
        /// 将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <returns>转换后的值</returns>
        protected override object PrimitiveConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType)
        {
            object result;
            switch (valueType)
            {
                case PrimitiveDataTypes.CharType:
                    result = (BigInteger)((char)value);
                    break;
                case PrimitiveDataTypes.ByteType:
                    result = (BigInteger)((byte)value);
                    break;
                case PrimitiveDataTypes.UInt16Type:
                    result = (BigInteger)((UInt16)value);
                    break;
                case PrimitiveDataTypes.UInt32Type:
                    result = (BigInteger)((UInt32)value);
                    break;
                case PrimitiveDataTypes.UInt64Type:
                    result = (BigInteger)((UInt64)value);
                    break;
                case PrimitiveDataTypes.UInt128Type:
                    result = (BigInteger)((UInt128)value);
                    break;
                case PrimitiveDataTypes.SByteType:
                    result = (BigInteger)((sbyte)value);
                    break;
                case PrimitiveDataTypes.Int16Type:
                    result = (BigInteger)((Int16)value);
                    break;
                case PrimitiveDataTypes.Int32Type:
                    result = (BigInteger)((Int32)value);
                    break;
                case PrimitiveDataTypes.Int64Type:
                    result = (BigInteger)((Int64)value);
                    break;
                case PrimitiveDataTypes.Int128Type:
                    result = (BigInteger)((Int128)value);
                    break;
                case PrimitiveDataTypes.FloatType:
                    result = (BigInteger)((float)value);
                    break;
                case PrimitiveDataTypes.DoubleType:
                    result = (BigInteger)((double)value);
                    break;
                case PrimitiveDataTypes.DecimalType:
                    result = (BigInteger)((decimal)value);
                    break;
                case PrimitiveDataTypes.EnumType:
                    IEnumParser enumParser = EnumParserManager.GetEnumParser(options.SourceEnumTypeName);
                    result = (BigInteger)(Convert.ToInt64(enumParser.GetEnumIntegerValue(value)));
                    break;
                case PrimitiveDataTypes.StringType:
                    if (options != null && options.Style is NumberStyles)
                    {
                        if (BigInteger.TryParse((string)value, (NumberStyles)options.Style, options.FormatProvider, out var v))
                        {
                            result = v;
                        }
                        else
                        {
                            throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"BigInteger\",转换失败");
                        }
                    }
                    else
                    {
                        if (options == null || options.FormatProvider == null)
                        {
                            if (BigInteger.TryParse((string)value, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"BigInteger\",转换失败");
                            }
                        }
                        else
                        {
                            if (BigInteger.TryParse((string)value, options.FormatProvider, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"BigInteger\",转换失败");
                            }
                        }
                    }
                    break;
                case PrimitiveDataTypes.BigIntegerType:
                    result = value;
                    break;
                default:
                    throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"{nameof(BigInteger)}\",转换失败");
            }
            return result;
        }

        /// <summary>
        /// 转换数据到
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <param name="result">转换后的值,存放在object中,如果转换失败为目标类型的默认值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType, out object result)
        {
            bool parseRet = true;
            switch (valueType)
            {
                case PrimitiveDataTypes.CharType:
                    result = (BigInteger)((char)value);
                    break;
                case PrimitiveDataTypes.ByteType:
                    result = (BigInteger)((byte)value);
                    break;
                case PrimitiveDataTypes.UInt16Type:
                    result = (BigInteger)((UInt16)value);
                    break;
                case PrimitiveDataTypes.UInt32Type:
                    result = (BigInteger)((UInt32)value);
                    break;
                case PrimitiveDataTypes.UInt64Type:
                    result = (BigInteger)((UInt64)value);
                    break;
                case PrimitiveDataTypes.UInt128Type:
                    result = (BigInteger)((UInt128)value);
                    break;
                case PrimitiveDataTypes.SByteType:
                    result = (BigInteger)((sbyte)value);
                    break;
                case PrimitiveDataTypes.Int16Type:
                    result = (BigInteger)((Int16)value);
                    break;
                case PrimitiveDataTypes.Int32Type:
                    result = (BigInteger)((Int32)value);
                    break;
                case PrimitiveDataTypes.Int64Type:
                    result = (BigInteger)((Int64)value);
                    break;
                case PrimitiveDataTypes.Int128Type:
                    result = (BigInteger)((Int128)value);
                    break;
                case PrimitiveDataTypes.FloatType:
                    result = (BigInteger)((float)value);
                    break;
                case PrimitiveDataTypes.DoubleType:
                    result = (BigInteger)((double)value);
                    break;
                case PrimitiveDataTypes.DecimalType:
                    result = (BigInteger)((decimal)value);
                    break;
                case PrimitiveDataTypes.EnumType:
                    IEnumParser enumParser = EnumParserManager.GetEnumParser(options.SourceEnumTypeName);
                    result = (BigInteger)(Convert.ToInt64(enumParser.GetEnumIntegerValue(value)));
                    break;
                case PrimitiveDataTypes.StringType:
                    if (options != null && options.Style is NumberStyles)
                    {
                        if (BigInteger.TryParse((string)value, (NumberStyles)options.Style, options.FormatProvider, out var v))
                        {
                            result = v;
                        }
                        else
                        {
                            result = null;
                            parseRet = false;
                        }
                    }
                    else
                    {
                        if (options == null || options.FormatProvider == null)
                        {
                            if (BigInteger.TryParse((string)value, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                result = null;
                                parseRet = false;
                            }
                        }
                        else
                        {
                            if (BigInteger.TryParse((string)value, options.FormatProvider, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                result = null;
                                parseRet = false;
                            }
                        }
                    }
                    break;
                case PrimitiveDataTypes.BigIntegerType:
                    result = value;
                    break;
                default:
                    result = null;
                    parseRet = false;
                    break;
            }

            return parseRet;
        }

    }
}
